$().ready(function() {
	tipoftheday_set_fileupload();
	addtipoftheday_set_fileupload();
	get_hash();
	$("#submitTipoftheday").click(function(){
		saveTipoftheday();
	});
	$("#add_tipoftheday_form").click(function(){
		addTipofthedayForm();
	});
	$("#addsubmitTipoftheday").click(function(){
		addTipoftheday();
	});
});
function list_tipoftheday(){
	var asort = 0;
	tipoftheday_icons = {};
	xfunc = "tipoftheday/list_tipoftheday";
	
	data_sent = {};
		
		$.ajax({ 
			type: "POST", // request type POST
			url: base_url+xfunc, // post to PHP file
			data: data_sent, // request send to PHP file
			dataType:'json',
			success: function(result){ // result = return from php request
				$(result.intID).each(function(k,v){
					asort = result.intID[k];
					var contVal = result.strNote[k];
					var desired = contVal.replace(/[^\w\s]/gi, '');
					tipoftheday_icons_panel = '';
						tipoftheday_icons_panel +='<tr>';
							tipoftheday_icons_panel +='<td><img class="img-rounded" src="'+base_url+'assets/uploaded/img/thumbnail/'+result.strImgLink[k]+'" alt=""/></td>';
							tipoftheday_icons_panel +='<td width="40">'+result.intNumDays[k]+'</td>';
							tipoftheday_icons_panel +='<td width="130">'+result.strNote[k]+'</td>';
							tipoftheday_icons_panel +='<td class="controls"><div class="btn-group">';
							tipoftheday_icons_panel +='<button class="btn btn-primary" onclick="javascript:editTipoftheday(\''+result.intID[k]+'\')"><i class="icon-edit"></i>Edit</button>';
							tipoftheday_icons_panel +='<button class="btn btn-danger" onclick="javascript:delTipoftheday(\''+result.intID[k]+'\', \''+desired+'\')"><i class="icon-trash"></i>Delete</button>';
							tipoftheday_icons_panel +='</td>';
						tipoftheday_icons_panel +='</tr>';
						tipoftheday_icons[result.intID[k]]=tipoftheday_icons_panel;

				});
				tipoftheday_html_panel = '<table class="table table-striped table-bordered" id="tipoftheday_table">';
				tipoftheday_html_panel += '<thead>';
					tipoftheday_html_panel += '<tr>';
						tipoftheday_html_panel += '<th class="thumb-photo">Thumbnail</th>';
						tipoftheday_html_panel += '<th class="note-title">Day</th>';
						tipoftheday_html_panel += '<th class="note-content">Tip Note</th>';
						tipoftheday_html_panel += '<th>Control</th>';
					tipoftheday_html_panel += '</tr>';
				tipoftheday_html_panel += '</thead>';
				tipoftheday_html_panel += '<tbody>';
				for(x=0;x<=asort;x++){
					if($.trim(tipoftheday_icons[x])!=""){
						tipoftheday_html_panel +=tipoftheday_icons[x];
					}	
				}		
				tipoftheday_html_panel += '</tbody>';		
				tipoftheday_html_panel += '</table>';
				
				$('#tipoftheday_content').html(tipoftheday_html_panel);
				$('#tipoftheday_table').dataTable({
					"sPaginationType": "full_numbers",
					"bLengthChange": false,
					"bSort": false
				});
			}
		});	
}
function addTipofthedayForm(){
	$("#tipoftheday_content, #add_tipoftheday_form").hide();
	$("#addTipoftheday").slideDown('slow', function(){
		$("#tipoftheday_content").hide();
		$("#addTipoftheday").slideDown('slow', function(){
			$(".back-btn-addtipoftheday a").attr("onclick","btn_back_addtipoftheday()");
		});	
	});	
}
function addTipoftheday(){
	var strNote = $("#addtipofthedaystrNotes").val();
	var intFontSize = $("#addintFontSize").val();
	var strImgLink = $("#addtipoftheday_thumbnailLink").val();
	var intNumDays = $("#addtipofthedayintNumDays").val();
	if(strNote=='') {
		$("#errorTipoftheday").html("<div class='alert alert-error'>Error: Put the Notes!</div>");
	} else if(strImgLink=='') {
		$("#errorTipoftheday").html("<div class='alert alert-error'>Error: Put the image thumbnail!</div>");
	} else if(intFontSize=='') {
		$("#errorTipoftheday").html("<div class='alert alert-error'>Error: Put the font size!</div>");
	} else {
		xfunc = "tipoftheday/add_tipoftheday";
		data_sent = {
				'strNote':strNote,
				'intFontSize':intFontSize,
				'strImgLink':strImgLink
		};
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				$("#errorTipoftheday").hide();
				$("#addtipofthedaySuccess").html('<div class="alert alert-success">Data was successfully saved!</div>');
				
				$("#addtipofthedaystrNotes").val('');
				$("#addintFontSize").val('');
				$("#addtipoftheday_thumbnailLink").val('');
				$("#addtipofthedayintNumDays").val('');
				$(".alert-success").fadeOut(5000);
			}
		});
	}
}
function editTipoftheday(noteID){
	xfunc = "tipoftheday/get_tipoftheday";
	data_sent = {
			'intID':noteID
	};
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			var tipoftheday_thumbnail = '<div class="imgThumbnail"><img class="img-rounded" width="86" src="'+base_url+'assets/uploaded/img/thumbnail/'+result.strImgLink+'" alt=""/></div>';
			$("#intTipofthedayID").val(noteID);
			$("#intNumDays").val(result.intNumDays);
			$("#strTipNote").val(result.strNote);
			$("#intFontSize").val(result.intFontSize);
			$("#tipoftheday_thumbnail").html(tipoftheday_thumbnail);
			$("#tipoftheday_thumbnailLink").val(result.strImgLink);
		}
	});

	$("#add_tipoftheday_form").hide();
	$("#tipoftheday_content").hide();
	$("#editTipoftheday").slideDown('slow', function(){
		$(".back-btn-tipoftheday a").attr("onclick","btn_back_tipoftheday()");
	});	
}
function saveTipoftheday(){
	var intID = $("#intTipofthedayID").val();
	var intNumDays = $("#intNumDays").val();
	var strNote = $("#strTipNote").val();
	var intFontSize = $("#intFontSize").val();
	var strImgLink = $("#tipoftheday_thumbnailLink").val();
	
	xfunc = "tipoftheday/save_tipoftheday";
	data_sent = {
			'intID':intID,
			'intNumDays':intNumDays,
			'strNote':strNote,
			'intFontSize':intFontSize,
			'strImgLink':strImgLink
	};
	$.ajax({
		type: 'POST',
		url: base_url+xfunc,
		data: data_sent, 
		dataType:'json',
		success: function(result){
			$("#tipofthedaySuccess").html('<div class="alert alert-success">Data was successfully updated!</div>');
			$(".alert-success").fadeOut(5000);
		}
	});
}
function delTipoftheday(noteID, titleName){
	xfunc = "tipoftheday/delete_tipoftheday";
	data_sent = {
			'intID':noteID
	};
	$('#label-delete').html(titleName);
    $('#deleteModal').modal('show');
    $('button#confirm').click(function(e){
        $('#deleteModal').modal('hide');
		
		$.ajax({
			type: 'POST',
			url: base_url+xfunc,
			data: data_sent, 
			dataType:'json',
			success: function(result){
				list_tipoftheday();
			}
		});
    });
}
function btn_back_addtipoftheday(){
	$("#addTipoftheday").hide();
	$("#tipoftheday_content").slideDown('slow', function(){
		$("#add_tipoftheday_form").show();
		list_tipoftheday();
	});	
}
function btn_back_tipoftheday(){
	$("#editTipoftheday").hide();
	$("#tipoftheday_content").slideDown('slow', function(){
		$("#add_tipoftheday_form").show();
		list_tipoftheday();
	});	
}
function tipoftheday_set_fileupload(){
	$('#tipoftheday_input_fileupload').attr('data-url',base_url+'assets/lib/jquery.fileupload/server/php/index.php');
	tipoftheday_file_upload();
}
function tipoftheday_file_upload(){
	$('.tipofthedayfileupload').fileupload();
	$('.tipofthedayfileupload').fileupload('option',{
		dataType: 'json',
		maxFileSize: 20000000, //20MB
		acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,			
		done: function (e, data) {
			$.each(data.result, function (index, file) {
				$.each(file, function (k, v) {		
					html = '<img width="86" class="img-rounded" src="'+base_url+"assets/uploaded/img/thumbnail/"+v.name+'" />';
					$("#tipoftheday_thumbnailLink").val(v.name);			
					$(".imgThumbnail").hide();
					$(".imgThumbnail").slideDown('slow', function(){
						$(".imgThumbnail").html(html);
					});					
				});
			});
		}
	}).bind('fileuploadfailed', function (e, data) {
		$(".result-message-box").html("Upload Error!");
	});		
}

//ADD DATA
function addtipoftheday_set_fileupload(){
	$('#addtipoftheday_input_fileupload').attr('data-url',base_url+'assets/lib/jquery.fileupload/server/php/index.php');
	addtipoftheday_file_upload();
}
function addtipoftheday_file_upload(){
	$('.addtipofthedayfileupload').fileupload();
	$('.addtipofthedayfileupload').fileupload('option',{
		dataType: 'json',
		maxFileSize: 20000000, //20MB
		acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,			
		done: function (e, data) {
			$.each(data.result, function (index, file) {
				$.each(file, function (k, v) {		
					html = '<img width="86" class="img-rounded" src="'+base_url+"assets/uploaded/img/thumbnail/"+v.name+'" />';
					$("#addtipoftheday_thumbnailLink").val(v.name);			
					$("#addtipoftheday_thumbnail").html(html);		
				});
			});
		}
	}).bind('fileuploadfailed', function (e, data) {
		$(".result-message-box").html("Upload Error!");
	});		
}
